﻿<%@ WebHandler Language="C#" Class="BargainMarket" %>



using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class BargainMarket : AjaxHandlerBase
{    
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //添加报备相关信息
                case "getlist":
                    GetList(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private void GetList(HttpContext context)
    {
        try
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" (1=1) ");
            //查询5级
            Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
            Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
            Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
            Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);

            if (departs != Guid.Empty)
            {
                sb.AppendFormat(" and (VenderDepartId = '{0}' or BuyerDepartId ='{0}' or (contractid in (select contractid from t_Contract_PointAllotInfo where DepartId='{0}')))", departs);
            }
            else if (businessdistrict != Guid.Empty)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                    if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
                }

                if (item == null)
                {
                    context.Response.Write(retExecResult(Constants.ExecResult.Error, "获取商圈或分类数据丢失", ""));
                }
                sb.AppendFormat(" and ((VenderDepartId in (Select DepartId from v_Hr_DepartmentInfo where DepartSrc like '{0}%')) or "
                    + "(BuyerDepartId in (Select DepartId from v_Hr_DepartmentInfo where DepartSrc like '{0}%')) or "
                    + "(contractid in (select contractid from t_Contract_PointAllotInfo where DepartId in (Select DepartId from v_Hr_DepartmentInfo where DepartSrc like '{0}%'))))", item.DepartSrc);
            }
            else if (company != Guid.Empty)
            {
                sb.AppendFormat(" and ((VenderCompanyId = '{0}') or (BuyerCompanyId='{0}') or (contractid in (select contractid from t_Contract_PointAllotInfo where EXISTS(select 1 from v_Hr_DepartmentInfo v where v.departid=t_Contract_PointAllotInfo.departid and v.companyid='{0}'))) )", company);
            }
            else if (city != Guid.Empty)
            {
                sb.AppendFormat(" and ((VenderCityId = '{0}') or (BuyerCityId='{0}') or (contractid in (select contractid from t_Contract_PointAllotInfo where EXISTS(select 1 from v_Hr_DepartmentInfo v where v.departid=t_Contract_PointAllotInfo.departid and v.CityId='{0}'))) )", city);
            }
            else
            {
                sb.AppendFormat(" and ((VenderCompanyId = '{0}') or (BuyerCompanyId='{0}') or (contractid in (select contractid from t_Contract_PointAllotInfo where EXISTS(select 1 from v_Hr_DepartmentInfo v where v.departid=t_Contract_PointAllotInfo.departid and v.companyid='{0}'))) )", CurrentUserInfo.CompanyId);
            }

            if (!string.IsNullOrEmpty(context.Request["BusinessType"]))
            {
                if (context.Request["BusinessType"] != "0")
                    sb.AppendFormat(" and BusinessType='{0}' ", context.Request["BusinessType"]);
            }
            if (!string.IsNullOrEmpty(context.Request["Region"]))
                sb.AppendFormat(" and Region='{0}' ", context.Request["Region"]);
            if (!string.IsNullOrEmpty(context.Request["HouseUse"]))
                sb.AppendFormat(" and HouseUse='{0}' ", context.Request["HouseUse"]);
            if (!string.IsNullOrEmpty(context.Request["BuildingType"]))
                sb.AppendFormat(" and BuildingType='{0}' ", context.Request["BuildingType"]);
            //时间
            if (!string.IsNullOrEmpty(context.Request["starttime_c"]))
                sb.AppendFormat(" and ContractDate>='{0}' ", context.Request["starttime_c"]);
            if (!string.IsNullOrEmpty(context.Request["endtime_c"]))
                sb.AppendFormat(" and ContractDate<='{0}' ", context.Request["endtime_c"]);
            if (!string.IsNullOrEmpty(context.Request["starttime_g"]))
                sb.AppendFormat(" and ArchivementDate>='{0}' ", context.Request["starttime_g"]);
            if (!string.IsNullOrEmpty(context.Request["endtime_g"]))
                sb.AppendFormat(" and ArchivementDate<='{0}' ", context.Request["endtime_g"]);
            //价格及面积

            double d = 0f;
            if (!string.IsNullOrEmpty(context.Request["areastar"]))
            {
                if (double.TryParse(context.Request["areastar"],out d))
                    sb.AppendFormat(" and Area>='{0}' ", d);
            }
            if (!string.IsNullOrEmpty(context.Request["areaend"]))
            {
                if (double.TryParse(context.Request["areaend"], out d))
                    sb.AppendFormat(" and Area<='{0}' ", d);
            }
            if (!string.IsNullOrEmpty(context.Request["pricestar"]))
            {
                if (double.TryParse(context.Request["pricestar"], out d))
                sb.AppendFormat(" and BargainPrice>='{0}' ", d);
            }
            if (!string.IsNullOrEmpty(context.Request["priceend"]))
            {
                if (double.TryParse(context.Request["priceend"], out d))
                sb.AppendFormat(" and BargainPrice<='{0}' ", d);
            }
            //关键字
            if (!string.IsNullOrEmpty(context.Request["keyword"]))
                sb.AppendFormat(" and (HouseAddress like '%{0}%' or Community like '%{0}%') ", context.Request["keyword"]);
            

            //排序
            string strOrderField = "ContractDate desc";
            if (!string.IsNullOrEmpty(context.Request.Params["sort"]))
                strOrderField = context.Request.Params["sort"];

            //条件
            string strWhere = sb.ToString();
            //页码
            int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            //页显示行数
            int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
            //欲选择字段列表 
            string FieldList = "*";
            //子查询欲选择字段列表
            string TableFieldList = "*";
            //所要查询的表名
            string TableName = "t_Contract_BaseInfo";
            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetList(strPageIndex, strPageSize, strOrderField, TableName, strWhere, FieldList, TableFieldList, true);

            context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[0].Rows.Count, Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0])));
            
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }
}